import { request } from '../request';

/** 估值系统API */

// 产品管理API
export function fetchProductTree() {
  return request<Api.Valuation.Product[]>({
    url: '/api/valuation/products/tree',
    method: 'get'
  });
}

export function fetchProductChildren(parentId: number) {
  return request<Api.Valuation.Product[]>({
    url: `/api/valuation/products/children/${parentId}`,
    method: 'get'
  });
}

export function fetchProductByCode(productCode: string) {
  return request<Api.Valuation.Product>({
    url: `/api/valuation/products/code/${productCode}`,
    method: 'get'
  });
}

export function fetchProductById(id: number) {
  return request<Api.Valuation.Product>({
    url: `/api/valuation/products/${id}`,
    method: 'get'
  });
}

export function createProduct(product: Api.Valuation.ProductDTO) {
  return request<Api.Valuation.Product>({
    url: '/api/valuation/products',
    method: 'post',
    data: product
  });
}

export function updateProduct(id: number, product: Api.Valuation.ProductDTO) {
  return request<Api.Valuation.Product>({
    url: `/api/valuation/products/${id}`,
    method: 'put',
    data: product
  });
}

export function deleteProduct(id: number) {
  return request({
    url: `/api/valuation/products/${id}`,
    method: 'delete'
  });
}

// export function scanProducts() {
//   return request<Api.Valuation.Product[]>({
//     url: '/api/valuation/products/scan',
//     method: 'post'
//   });
// }

export function enableProduct(id: number) {
  return request({
    url: `/api/valuation/products/${id}/enable`,
    method: 'put'
  });
}

export function disableProduct(id: number) {
  return request({
    url: `/api/valuation/products/${id}/disable`,
    method: 'put'
  });
}

export function validateProductCode(productCode: string, excludeId?: number) {
  return request<{ isUnique: boolean }>({
    url: '/api/valuation/products/validate-code',
    method: 'get',
    params: { productCode, excludeId }
  });
}

export function getProductIdByClassName(className: string) {
  return request<number>({
    url: `/api/valuation/product-definition/by-class-name/${className}`,
    method: 'get'
  });
}

// 模型管理API
export function fetchModelByCode(modelCode: string) {
  return request<Api.Valuation.Model>({
    url: `/api/valuation/models/code/${modelCode}`,
    method: 'get'
  });
}

export function fetchModelById(id: number) {
  return request<Api.Valuation.Model>({
    url: `/api/valuation/models/${id}`,
    method: 'get'
  });
}

export function updateModel(id: number, model: Api.Valuation.ModelDTO) {
  return request<Api.Valuation.Model>({
    url: `/api/valuation/models/${id}`,
    method: 'put',
    data: model
  });
}

export function deleteModel(id: number) {
  return request({
    url: `/api/valuation/models/${id}`,
    method: 'delete'
  });
}

export function enableModel(id: number) {
  return request({
    url: `/api/valuation/models/${id}/enable`,
    method: 'put'
  });
}

export function disableModel(id: number) {
  return request({
    url: `/api/valuation/models/${id}/disable`,
    method: 'put'
  });
}

// 模型列表API
export function fetchModels() {
  return request<Api.Valuation.Model[]>({
    url: '/api/valuation/models/discovered',
    method: 'get'
  });
}

// 新增：模型自动发现/同步
export function discoverModels() {
  return request<Api.Valuation.Model[]>({
    url: '/api/valuation/model-discovery/discover',
    method: 'get'
  });
}

export function listDiscoveredModels() {
  return request<Api.Valuation.Model[]>({
    url: '/api/valuation/models/discovered',
    method: 'get'
  });
}

// 产品试算API
export function trialCalculateProduct(trialRequest: any) {
  return request<any>({
    url: `/api/valuation/product-definition/trial-calculation`,
    method: 'post',
    data: trialRequest
  });
}

// 参数管理API
export function fetchAllParameters() {
  return request<Api.Valuation.Parameter[]>({
    url: '/api/valuation/parameters',
    method: 'get'
  });
}

export function fetchParametersByModelId(modelId: number) {
  return request<Api.Valuation.Parameter[]>({
    url: `/api/valuation/parameters/model/${modelId}`,
    method: 'get'
  });
}

export function fetchParametersByModelIdReflection(modelId: number) {
  return request<Api.Valuation.Parameter[]>({
    url: `/api/valuation/models/${modelId}/parameters/reflection`,
    method: 'get'
  });
}

export function fetchRequiredParametersByModelId(modelId: number) {
  return request<Api.Valuation.Parameter[]>({
    url: `/api/valuation/parameters/model/${modelId}/required`,
    method: 'get'
  });
}

export function fetchOptionalParametersByModelId(modelId: number) {
  return request<Api.Valuation.Parameter[]>({
    url: `/api/valuation/parameters/model/${modelId}/optional`,
    method: 'get'
  });
}

export function fetchParametersByType(type: string) {
  return request<Api.Valuation.Parameter[]>({
    url: `/api/valuation/parameters/type/${type}`,
    method: 'get'
  });
}

export function fetchParameterById(id: number) {
  return request<Api.Valuation.Parameter>({
    url: `/api/valuation/parameters/${id}`,
    method: 'get'
  });
}

export function createParameter(parameter: Api.Valuation.ParameterDTO) {
  return request<Api.Valuation.Parameter>({
    url: '/api/valuation/parameters',
    method: 'post',
    data: parameter
  });
}

export function updateParameter(id: number, parameter: Api.Valuation.ParameterDTO) {
  return request<Api.Valuation.Parameter>({
    url: `/api/valuation/parameters/${id}`,
    method: 'put',
    data: parameter
  });
}

export function deleteParameter(id: number) {
  return request({
    url: `/api/valuation/parameters/${id}`,
    method: 'delete'
  });
}

// 估值任务管理API
export function createValuationTask(params: {
  productId: number;
  modelId: number;
  taskType: string;
  marketDataSet: Record<string, any>;
}) {
  return request<{ taskId: string; success: boolean }>({
    url: '/api/valuation/preparation/tasks',
    method: 'post',
    params: {
      productId: params.productId,
      modelId: params.modelId,
      taskType: params.taskType
    },
    data: params.marketDataSet
  });
}

export function executeUAMCTask(taskId: string) {
  return request<{ success: boolean }>({
    url: `/api/valuation/preparation/tasks/${taskId}/execute-uamc`,
    method: 'post'
  });
}

export function executeUACATask(taskId: string) {
  return request<{ success: boolean }>({
    url: `/api/valuation/preparation/tasks/${taskId}/execute-uaca`,
    method: 'post'
  });
}

export function executeYSTask(taskId: string) {
  return request<{ success: boolean }>({
    url: `/api/valuation/preparation/tasks/${taskId}/execute-ys`,
    method: 'post'
  });
}

export function executeUASTask(taskId: string) {
  return request<{ success: boolean }>({
    url: `/api/valuation/preparation/tasks/${taskId}/execute-uas`,
    method: 'post'
  });
}

export function updateTaskStatus(taskId: string, status: string, progress: number = 0) {
  return request<{ success: boolean }>({
    url: `/api/valuation/preparation/tasks/${taskId}/status`,
    method: 'put',
    params: { status, progress }
  });
}

export function updateTaskResult(
  taskId: string,
  params: {
    taskStatus: string;
    result: Record<string, any>;
    errorMessage?: string;
  }
) {
  return request<{ success: boolean }>({
    url: `/api/valuation/preparation/tasks/${taskId}/result`,
    method: 'put',
    params: {
      taskStatus: params.taskStatus,
      errorMessage: params.errorMessage
    },
    data: params.result
  });
}

export function cancelTask(taskId: string) {
  return request<{ success: boolean }>({
    url: `/api/valuation/preparation/tasks/${taskId}/cancel`,
    method: 'post'
  });
}

export function retryTask(taskId: string) {
  return request<{ success: boolean }>({
    url: `/api/valuation/preparation/tasks/${taskId}/retry`,
    method: 'post'
  });
}

export function executeBatchValuationPreparation(params: {
  productId: number;
  modelId: number;
  marketDataSet: Record<string, any>;
}) {
  return request<{ success: boolean; taskIds: string[] }>({
    url: '/api/valuation/preparation/batch-execute',
    method: 'post',
    params: {
      productId: params.productId,
      modelId: params.modelId
    },
    data: params.marketDataSet
  });
}

// 数据库验证API
export function verifyTables() {
  return request<Record<string, any>>({
    url: '/api/database/verify-tables',
    method: 'get'
  });
}

export function fetchSampleProducts() {
  return request<Record<string, any>>({
    url: '/api/database/sample-products',
    method: 'get'
  });
}

export function fetchSampleModels() {
  return request<Record<string, any>>({
    url: '/api/database/sample-models',
    method: 'get'
  });
}

export function fetchSampleTasks() {
  return request<Record<string, any>>({
    url: '/api/database/sample-tasks',
    method: 'get'
  });
}

export function getProductDefinitionDetail(className: string) {
  return request<Api.Valuation.ProductDefinitionDetail>({
    url: `/api/valuation/product-definition/detail/${className}`,
    method: 'get'
  });
}

export function getProductDefinitionFields(className: string) {
  return request<Api.Valuation.ProductField[]>({
    url: `/api/valuation/product-definition/fields/${className}`,
    method: 'get'
  });
}

export function validateProductInstance(className: string, productData: Record<string, any>) {
  return request<any>({
    url: `/api/valuation/product-definition/validate/${className}`,
    method: 'post',
    data: productData
  });
}

// 测试用例报文API
export function fetchTestMessage(productType: string) {
  return request<string>({
    url: `/api/testcase/message/${productType}`,
    method: 'get'
  });
}

export function fetchTestCasesByProductType(productType: string) {
  return request<any[]>({
    url: `/api/testcase/list/${productType}`,
    method: 'get'
  });
}

export function fetchAllProductTypes() {
  return request<string[]>({
    url: '/api/testcase/product-types',
    method: 'get'
  });
}

export function fetchMarketDataById(id: string) {
  return request<any>({
    url: '/api/market-data/fetch-market-by-id',
    method: 'post',
    data: id
  });
}

// 重新导出产品模型映射相关函数
export {
  createProductModelMapping,
  createProductModelMappingWithDataSet,
  updateProductModelMapping,
  deleteProductModelMapping,
  fetchProductModelMappings,
  fetchProductModelMappingsByDataSetId,
  fetchAllMappingsByDataSetId,
  fettchProductDefinitions
} from './product-model-mapping';
